
<template>
  <div style="height: 100%; width: 100%">
    <div ref="basicMapbox" :style="mapSize"></div>
  </div>
</template>
<script>
//import mapboxgl from "mapbox-gl";
import MapboxLanguage from "@mapbox/mapbox-gl-language";
export default {
  props: {
    mapWidth: {
      type: String,
    },
    mapHeight: {
      type: String,
    },
  },
  data() {
    return {};
  },
  mounted() {
    this.init();
  },
  activated() {
    //只刷新数据，不改变整体的缓存
    this.init();
 },
  methods: {
    // 初始化
    init() {
      let mapboxgl = this.$mapboxgl
      const map = new mapboxgl.Map({
        container: this.$refs.basicMapbox,
        style: "mapbox://styles/mapbox/dark-v10",
        center: [113.901249, 22.563841],
        zoom: 15,
      });
      // 设置语言
      var language = new MapboxLanguage({ defaultLanguage: "zh-Hans" });
      map.addControl(language);

      // 地图导航
      var nav = new mapboxgl.NavigationControl();
      map.addControl(nav, "bottom-right");
      // 比例尺
      var scale = new mapboxgl.ScaleControl({
        maxWidth: 80,
        unit: "imperial",
      });
      map.addControl(scale);
      scale.setUnit("metric");
      // 全图
      /*
      map.addControl(new mapboxgl.FullscreenControl());
      */
      // 定位
      map.addControl(
        new mapboxgl.GeolocateControl({
          positionOptions: {
            enableHighAccuracy: true,
          },
          trackUserLocation: true,
        })
      );
      // console.log(map)
    },
  },
  computed: {
    mapSize() {
      let styleObj = {
        width: this.mapWidth,
        height: this.mapHeight,
      };
      return styleObj;
    },
  },
};
</script>
<style >
@import url("https://api.tiles.mapbox.com/mapbox-gl-js/v0.51.0/mapbox-gl.css");
.mapboxgl-map {
  height: 100%;
  width: 100%;
}
.mapboxgl-canvas {
  outline: none;
}

.mapboxgl-ctrl-logo {
  display: none;
}
</style>
